<template>
  <el-dialog :title="showTitle" :visible="showDialogv" @close="btnCancel">
    <el-form ref="deptsFrom" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" width="80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" width="80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" width="80%" placeholder="请选择" @focus="getEmployeeSimple">
          <el-option v-for="item in people" :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" width="80%" type="textarea" :rows="3" placeholder="1-300个字符" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  props: {
    showDialogv: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: Object,
      default: null
    }
  },
  data() {
    // 检查部门名称是否重复
    const checkNameRepeat = async(rule, value, callback) => {
    //   // value就是部门名称，要去和同级的比较是否有相同的
      const { depts } = await getDepartments()
      let isRepeat = false
      if (this.formData.id) {
        // 编辑状态下
        // 先找到同级部门下，再排除自己,同级部门下-姓名不能重复
        isRepeat = depts.filter(item => item.id !== this.formData.id && item.pid === this.treeNode.pid).some(item => item.name === value)
      } else {
        // 新增状态下
        isRepeat = depts.filter(item => item.pid === this.treeNode.id).some(item => item.name === value)
      // console.log(isRepeat)
      }
      isRepeat ? callback(new Error(`同级部门已存在${value}这个部门了`)) : callback()
    }
    const checkCodeRepeat = async(rule, value, callback) => {
      const { depts } = await getDepartments()
      let isRepeat = false
      if (this.formData.id) {
        isRepeat = depts.some(item => item.id !== this.formData.id && item.code === value && value)
      } else {
        isRepeat = depts.some(item => item.code === value && value)
      }
      isRepeat ? callback(new Error(`同级部门已存在${value}这个部门编码了`)) : callback()
    }
    return {

      // 定义表单数据
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门负责人
        introduce: '' // 部门介绍
      },
      people: [], // 下拉列表员工数组
      // 定义校验规则
      rules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' }, { min: 1, max: 50, message: '部门名称长度在1-50个字符之间', trigger: 'blur' }, { trigger: 'blur', validator: checkNameRepeat }],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' }, { min: 1, max: 50, message: '部门编码长度在1-50个字符之间', trigger: 'blur' }, { trigger: 'blur', validator: checkCodeRepeat }],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }, { min: 1, max: 50, message: '部门负责人长度在1-50个字符之间', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' }, { min: 1, max: 300, message: '部门介绍长度在1-300个字符之间', trigger: 'blur' }]
      }
    }
  },
  // 计算属性
  computed: {
    showTitle() {
      return this.formData.id ? '编辑部门' : '新增子部门'
    }
  },
  // 方法
  methods: {
    async getEmployeeSimple() {
      this.people = await getEmployeeSimple()
    },
    async getDepartDetail(id) {
      this.formData = await getDepartDetail(id)
    },
    btnOk() {
      this.$refs.deptsFrom.validate(async isOk => {
        // console.log(isOk)
        if (isOk) {
          if (this.formData.id) {
            // 编辑
            await updateDepartments(this.formData)
          } else {
            // 新增
            await addDepartments({ ...this.formData, pid: this.treeNode.id })
          }
          // 告诉父组件
          this.$emit('addDepts')
          this.$emit('update:showDialog123', false)
        }
      })
    },
    btnCancel() {
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门负责人
        introduce: '' // 部门介绍
      }
      // 关闭弹层
      this.$emit('update:showDialog123', false)
      // 重置数据
      this.$refs.deptsFrom.resetFields()
    }
  }
  // created() {
  //   this.Fn()
  // }
  // methods: {
  // async Fn() {
  // value就是部门名称，要去和同级的比较是否有相同的
  // const { depts } = await getDepartments()
  // const isRepeat = depts.filter(item => item.pid === this.treeNode.id)
  // console.log(depts)
  // isRepeat ? callback(new Error(`同级部门已存在${value}这个部门了`)) : callback()
  // }
  // }
}
</script>

<style>

</style>
